package com.jhhc.Weathers.datasupplement.util;

import com.jhhc.Weathers.datasupplement.entity.zksgBO.PathFile;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class FileUtil {

    /**
     * 判断文件或者文件夹是否存在
     * @param path 路径
     * @param flag true 文件 false文件夹
     */
    public static boolean isFile(String path,boolean flag){
        if (flag) {
            return new File(path).exists();
        }else {
            return new File(path).exists();
        }
    }

    public List<PathFile> ftpPaths(String time, String localPath, String ftpPath){
        List<PathFile> files = new ArrayList<>();
        PathFile p1 = new PathFile();
        p1.setFtpPath(ftpPath + "/" + time + "/PRATEsfc.nc");
        p1.setLocalPath(localPath + File.separator + time);
        p1.setFileName("PRATEsfc.nc");
        files.add(p1);
        PathFile p2 = new PathFile();
        p2.setFtpPath(ftpPath + "/" + time + "/slp.nc");
        p2.setLocalPath(localPath + File.separator + time);
        p2.setFileName("slp.nc");
        files.add(p2);
        PathFile p3 = new PathFile();
        p3.setFtpPath(ftpPath + "/" + time + "/UGRD10m.nc");
        p3.setLocalPath(localPath + File.separator + time);
        p3.setFileName("UGRD10m.nc");
        files.add(p3);
        PathFile p4 = new PathFile();
        p4.setFtpPath(ftpPath + "/" + time + "/VGRD10m.nc");
        p4.setLocalPath(localPath + File.separator + time);
        p4.setFileName("VGRD10m.nc");
        files.add(p4);
        return files;
    }

    public int download(String host,String username,String password,int port,
                         List<PathFile> ftpPaths){
        int r = -1;
        long startTime = System.currentTimeMillis();
        FTPUtil ftpUtil = new FTPUtil();
        FTPClient ftpClient = ftpUtil.getFTPClient(host,username,password,port);
        File file = new File(ftpPaths.get(0).getLocalPath());
        if (!file.exists()) {
            for (PathFile ftpPath : ftpPaths) {
                int result = ftpUtil.downloanFtpFlie(ftpClient,ftpPath.getFtpPath(),ftpPath.getLocalPath(),
                        ftpPath.getFileName());
                if (result == 1) {
                    System.out.println(ftpPath.getFileName() + "下载完成");
                    r = 1;
                }else {
                    System.out.println(ftpPath.getFileName() + "下载失败");
                }
            }
        }
        // 断开连接
        try {
            ftpClient.logout();
            ftpClient.disconnect();
            System.out.println("断开连接");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("ftp异常");
        }
        long endTime = System.currentTimeMillis();
        float excTime = (float)(endTime - startTime) / 1000;
        System.out.println("下载文件耗时：" + excTime + "s");
        return r;
    }

    public void fileUpload(String host,String username,String password,int port,
                           String ftpPath,String localPath){
        long startTime = System.currentTimeMillis();
        FTPUtil ftpUtil = new FTPUtil();
        FTPClient ftpClient = ftpUtil.getFTPClient(host,username,password,port);
        int result = ftpUtil.fileUpload(ftpClient,ftpPath,localPath);
        if (result == 1) {
            System.out.println(localPath + "上传完成");
        }else {
            System.out.println(localPath + "上传失败");
        }
        try {
            ftpClient.logout();
            ftpClient.disconnect();
            System.out.println("断开连接");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("ftp异常");
        }
        long endTime = System.currentTimeMillis();
        float excTime = (float)(endTime - startTime) / 1000;
        System.out.println("上传文件耗时：" + excTime + "s");
    }

}
